package com.smart4s.app.core.positionservice.activity;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.smart4s.app.core.positionservice.R;
import com.smart4s.app.core.positionservice.base.BaseActivity;
import com.smart4s.app.core.positionservice.base.BasePresenter;
import com.smart4s.app.core.positionservice.presenter.PresenterImpl;
import com.smart4s.app.core.positionservice.service.LightDecoderService;
import com.smart4s.app.core.positionservice.utils.ConfigManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 地图界面
 */
public class MainActivity extends AppCompatActivity {

    private static final String ACTION_START = "1";
    public static TextView mIdtext;
    public static TextView mXtext, mYtext;
    public static TextView mMoblieAzitext;
    public static String lid;
    public static String x;
    public static String y;
    public static String azi;
    public static SharedPreferences sp;
    private static volatile boolean mChangeColor = false;
    private int mCount = 0;
    //解码是否成功
    private boolean isDecodeSuccess = false;
    // 是否请求解码
    private boolean isRequestDecode = true;
    private boolean mIsConnected;

    private ServiceConnection mLightServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIsConnected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mIsConnected = false;
        }
    };
    public static SharedPreferences.Editor edit;
    public static TextView mFloarText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }).start();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        try {
            ConfigManager.releaseAssetsMarketData(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

        mIdtext = findViewById(R.id.id_text);
        mXtext = findViewById(R.id.x_text);
        mYtext = findViewById(R.id.y_text);
        mMoblieAzitext = findViewById(R.id.mobileAzi_text);
        mFloarText = (TextView) findViewById(R.id.floar_text);

        // 开启光定位
        Intent intent = new Intent(this, LightDecoderService.class);
        bindService(intent, mLightServiceConnection, Context.BIND_AUTO_CREATE);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (mIsConnected) {
        unbindService(mLightServiceConnection);
//            mIsConnected = false;
//        }
        EventBus.getDefault().unregister(this);
        actionStart(this);
    }

    /**
     * 光定位解码回调
     *
     * @param lightLocationResult
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLightLocationResult(LightDecoderService.LightLocationResult lightLocationResult) {
        lid = lightLocationResult.lid;
        x = String.valueOf((float) ((int) (lightLocationResult.x * 1000)) / 1000);
        y = String.valueOf((float) ((int) (lightLocationResult.y * 1000)) / 1000);
        azi = String.valueOf((float) ((int) (lightLocationResult.mobileAzi * 1000)) / 1000);

        Log.d("lightLocationResult",
                " ID: " + lightLocationResult.lid +
                        " x: " + lightLocationResult.x +
                        " y: " + lightLocationResult.y +
                        " M_theta: " + lightLocationResult.M_theta +
                        " mobileAzi: " + lightLocationResult.mobileAzi);

        //存储数据
        sp = getSharedPreferences("service", MODE_PRIVATE);
        edit = sp.edit();

        mIdtext.setText(lid);
        mXtext.setText(x);
        mYtext.setText(y);
        mMoblieAzitext.setText(azi);


        Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (lid.equals("0x00000001")) {
                    edit.clear();
                } else {
                    edit.clear();
                    edit.putString("mapId", lid);
                    edit.putString("mapX", x);
                    edit.putString("mapY", y);
                    edit.putString("mapMobileAzi", azi);
                    edit.putInt("mapFloor", LightDecoderService.LightLocationResult.floor);
                    edit.commit();
                }
            }
        };
        timer.schedule(timerTask, 0, 3000);


        if (!lightLocationResult.lid.equals(LightDecoderService.SIGHT_LID)) {
            mIdtext.setText(lid);
            mXtext.setText(x);
            mYtext.setText(y);
            mMoblieAzitext.setText(azi);
            mFloarText.setText(LightDecoderService.LightLocationResult.floor);
            if (mCount % 2 == 1) {
                mIdtext.setTextColor(Color.parseColor("#00FF00"));
                mXtext.setTextColor(Color.parseColor("#00FF00"));
                mYtext.setTextColor(Color.parseColor("#00FF00"));
                mMoblieAzitext.setTextColor(Color.parseColor("#00FF00"));
                mFloarText.setTextColor(Color.parseColor("#00FF00"));
            } else {
                mIdtext.setTextColor(Color.parseColor("#FF0000"));
                mXtext.setTextColor(Color.parseColor("#FF0000"));
                mYtext.setTextColor(Color.parseColor("#FF0000"));
                mMoblieAzitext.setTextColor(Color.parseColor("#FF0000"));
                mFloarText.setTextColor(Color.parseColor("#FF0000"));
            }
            mCount++;
        }
    }

    private String HextoDe(String Hex) {
        int d = Integer.valueOf(Hex, 16);
        String de = String.valueOf(d);
        return de;
    }

    int switchColor() {
        // int seed = -(int) (Math.random() * 16777215 + 1);
        // String hexString = Integer.toHexString(seed);
        // 红色#FF0000 绿色 #00FF00
        int color = Color.parseColor("#FF0000");
        if (mChangeColor) {
            color = Color.parseColor("#00FF00");
        }
        mChangeColor = !mChangeColor;
        return color;
    }

    public static void actionStart(Context ctx) {
        //System.out.println("---- Notification service started!");
        Intent i = new Intent(ctx, LightDecoderService.class);
        i.setAction(ACTION_START);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx.startService(i);
    }

}